package com.hd.aop0410.controller;

import com.hd.aop0410.vo.UserVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.*;

@RestController
@RequestMapping("/param")
@Validated//开启参数校验功能
public class ParamController {
    @GetMapping("/login")
    public String login(@NotEmpty(message = "不能是空串")  @NotNull @Size(min = 5,max = 10) String name,
                        @NotEmpty(message = "不能是空串")  @NotNull @Size(min = 6,max =20)String password,
                        @Email String email,
                        //正则表达式
                        @Pattern(regexp = "^[1-9]\\d{5}(?:18|19|20)\\d{2}(?:0\\d|10|11|12)(?:0[1-9]|[1-2]\\d|30|31)\\d{3}[\\dXx]$") String sfz,
                        @Min(1) @Max(150) int age) throws Exception {
     //1.参数校验
     // name不能为空，字符串长度在5-10之间
     //password不能为空，长度在6-20之间
         /*if (name==null){
             throw  new Exception("用户名不能为空");
         }
         if (password==null){
            throw  new Exception("密码不能为空");
         }
         if (name.length()<5||name.length()>10){
             throw  new Exception("用户名长度不符合要求");
         }*/
        return "login success";
    }
    @PostMapping("/regiest")
    public String regiest(@Valid UserVo userVo){//开启对象收参的校验功能
        System.out.println(userVo);
        return "regiest success";

    }

}
